Skip to content

impl(atlas): D1 hot-cache freshness evaluator — per-class watermark/TTL rules (#13101) - #13133

Open
dezren39 wants to merge 1 commit into
mainfrom
xaltha/13101-atlas-freshness
Open

dezren39 wants to merge 1 commit into
mainfrom
xaltha/13101-atlas-freshness

Conversation

@dezren39

@dezren39 dezren39 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Follow-on 2 of 6 under #9134 (Atlas D1 tenant hot cache design, merged via PR #13098), implementing §1 + §8 item 2 in pkgs/atlas-search/worker. Based on main — no code dependence on follow-on 1 (#13100, the D1 store).

What landed

  • src/freshness.ts — the pure freshness evaluator. No I/O, no clock reads: the caller supplies now and the watermark read result.
    • Class A (content-addressed payloads): the hash is the version — fresh on hash match at any age, watermark state irrelevant; a hash mismatch (or an entry with no hash to prove identity) is never served.
    • Class B (query result sets): fresh iff the entry's source_watermark still equals the tenant's current watermark and age ≤ TTL_B (default 60 s) — whichever fails first, so a watermark advance is stale even inside the TTL.
    • Class C (tenant aggregates): fresh iff age ≤ TTL_C (default 15 min) and no invalidation event for the tenant post-dates the entry.
    • Fail-safe degradation (§1): watermark source unreachable → Class B degrades to TTL-only with TTL_B divided by the degraded divisor (default 2 → 30 s, floored, clamped ≥ 1 so it can never extend), Class C stops being served, Class A unaffected. A future filled_at (clock skew) is never fresh for the TTL classes.
    • The tenant watermark read: WatermarkSource seam + readTenantWatermark, mapping every source failure (throw, null) to unavailable — unknown can only narrow the bound, never widen it. evaluateWithWatermarkRead composes read + pure evaluation for the read path.
    • Bounds are named config (ttlBSeconds, ttlCSeconds, degradedDivisor) with the design's defaults; invalid overrides fall back to the defaults.
    • Verdicts carry a reason (the §2 skip-log surface), the age, the applied bound, and a degraded flag.
  • test/freshness.test.ts — 23 tests. The named oracle: property tests over generated (watermark, age, class, event) sequences (seeded, reproducible; 200 sequences × up to 12 events across 3 configs) asserting exact agreement with an independent transcription of the §1 table — the stale-serves counter is asserted zero, and so is the over-refusal counter. Plus unit tests for each named rule, config resolution, and the watermark read.
  • README.md — Shape section gains the module, noting it is not wired into a read path yet (store is impl(atlas): D1 hot-cache schema, binding, and store module for the atlas-search Worker #13100, integration is follow-on 4).

Test evidence

The worker package's node_modules are not installed in the sandbox (and installing was out of bounds for this lane), so the committed vitest file was run byte-for-byte under bun test with a vitest→bun:test shim, in a scratch dir outside the repo:

  • New suite: 23 pass / 0 fail, 1398 expect() calls (the property oracle alone makes >1000 evaluations).
  • Existing runtime-independent suites (key, body, space): 15 pass / 0 fail under the same shim.
  • mcp/worker suites import cloudflare:test and need the Cloudflare vitest pool — not runnable in the sandbox; their files are untouched (this PR only adds files plus one README bullet).
  • Strict typecheck (tsc 5.9.3, --strict --skipLibCheck, package-equivalent flags): both new files pass.
  • CI (atlas-search-worker-tests) runs the real vitest pool suite and is the first run of the new tests under vitest proper.

Closes #13101


Show it working
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…TL rules (#13101)

Follow-on 2 of 6 under #9134 (design merged via PR #13098), §1 + §8
item 2: the pure freshness evaluator for the D1 tenant hot cache.

- Class A: the content hash is the version — fresh on hash match at
  any age, watermark state irrelevant; a mismatch is never served.
- Class B: fresh iff the entry's source_watermark still equals the
  tenant's current watermark AND age <= TTL_B (60 s) — whichever
  fails first; a watermark advance is stale even inside the TTL.
- Class C: fresh iff age <= TTL_C (15 min) AND no invalidation
  event for the tenant post-dates the entry.
- Fail-safe degradation: watermark source unreachable -> Class B
  TTL-only with TTL_B halved (divisor config, floored, clamped so
  it can never extend), Class C not served, Class A unaffected.
  Clock skew (future filled_at) is never fresh for the TTL classes.
- The tenant watermark read is a thin seam (WatermarkSource) that
  maps every source failure to 'unavailable'; the evaluator itself
  is pure — no I/O, no clock reads.
- Bounds are named config (TTL_B, TTL_C, degraded divisor) with the
  design's defaults; invalid overrides fall back to the defaults
  and can never widen a bound.

Oracle (per the issue): property tests over generated (watermark,
age, class, event) sequences assert exact agreement with an
independent transcription of the §1 table; the stale-serves
counter is asserted zero (200 sequences, >1000 evaluations, plus
22 unit tests for the named rules). README Shape gains the module.

Verification (sandbox): the worker package's node_modules are not
installed in the sandbox, so the committed vitest file was run
byte-for-byte under bun test with a vitest->bun:test shim: 23/23
pass, 1398 expect() calls; the package's runtime-independent
suites (key, body, space) also pass 15/15 under the same shim;
mcp/worker suites need the Cloudflare pool and are untouched.
Strict tsc (5.9.3, package-equivalent flags) passes on both new
files. CI runs the real vitest pool suite.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 4 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cb5bce32-9b03-4e8c-81d0-8b6c30b37e77
Commits

Reviewing files that changed from the base of the PR and between 7153a15 and 52315ba.

Files selected for processing: 3

  • pkgs/atlas-search/worker/README.md
  • pkgs/atlas-search/worker/src/freshness.ts
  • pkgs/atlas-search/worker/test/freshness.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T08:56:51.005140Z 52315ba PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

impl(atlas): D1 hot-cache freshness evaluator — per-class watermark/TTL rules

1 participant